package com.epam.rentcar.controller.actions;

import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.epam.rentcar.model.CarRent;

public class RequestCarAction implements Action {

	private static Logger log = Logger.getLogger(RequestCarAction.class);

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {		

		CarRent carRentAppModel = (CarRent) request.getSession().getAttribute("carRentAppModel");

		try {
			if (carRentAppModel.hasAvaliableCars()){
				return "/request.jsp";
			}
		} catch (SQLException | NamingException | NullPointerException e) {
			log.error(e);
			throw new ServletException(e);
		}

		request.setAttribute("message", "no avaliable cars");
		return "/request.jsp";
	}

}
